//sort.js
const db = wx.cloud.database()
const substance = db.collection('substance')
const user = db.collection('user')
const star = db.collection('star')
const _ = db.command
//獲取應用實例
var app = getApp
Page({

  /**
   * 页面的初始数据
   */
  data: {
    columnsBook: ['计算机', '机电', '自动化', '轻化', '信工', '土木与交通', '管理', '材能', '环境', '外国语', '应数', '物理', '艺设', '政法', '建筑城规', '经贸', '生物医学', '国际教育'],
    columnsGood: ['电子产品', '体育用品', '生活用品', '其他'],
    goodList: null,
    starList:null,
    bind:"未认证",
    //用户个人信息
    userInfo: {
      avatarUrl: "",//用户头像
      nickName: "",//用户昵称
    },
    active: 0
  },
  /**
   *点击添加地址事件
   */
  handleCardEntry(e) {
    wx.navigateTo({
      url: '../show/detailed/detailed?_id=' + e.currentTarget.dataset._id + '&_openid=' + e.currentTarget.dataset._openid
    }) // 带参跳转
  },
  handleCardEntry1(e){
    wx.navigateTo({
      url: '../show/detailed/detailed?_id=' + e.currentTarget.dataset.starid + '&_openid=' + e.currentTarget.dataset.staropenid
    })
  },
  onChange(event) {
    let self = this;
    if (!event.detail.index) {
      wx.getStorage({
        key: 'openid',
        success: function (res) {
          substance.where({
            _openid: res.data
          }).get({
            success: res => {
              let goodList = res.data
              let handleTime = (date) => {
                let nowTime = (new Date()).valueOf()
                if (parseInt((nowTime - date) / 2592000000) >= 1) {
                  return parseInt((nowTime - date) / 2592000000) + '月前'
                } else if (parseInt((nowTime - date) / 86400000) >= 1) {
                  return parseInt((nowTime - date) / 86400000) + '天前'
                } else if (parseInt((nowTime - date) / 3600000) >= 1) {
                  return parseInt((nowTime - date) / 3600000) + '小时前'
                } else if (parseInt((nowTime - date) / 60000) >= 1) {
                  return parseInt((nowTime - date) / 60000) + '分钟前'
                } else {
                  return '刚刚'
                }
              }
              for (let i = 0; i < goodList.length; i++) {
                if (goodList[i].tip == 'good') {
                  goodList[i].type = self.data.columnsGood[goodList[i].type];
                } else {
                  goodList[i].type = self.data.columnsBook[goodList[i].type];
                }
                goodList[i].date = handleTime(goodList[i].date)
              }
              // res.data 是包含以上定义的两条记录的数组
              self.setData({
                goodList: goodList.reverse()
              })
            }
          })
        },
      })
    }else{
      wx.getStorage({
        key: 'openid',
        success: function(res) {
          star.where({
              _openid:res.data
          }).get({
              success: res => {
                let starList = res.data
                let handleTime = (date) => {
                  let nowTime = (new Date()).valueOf()
                  if (parseInt((nowTime - date) / 2592000000) >= 1) {
                    return parseInt((nowTime - date) / 2592000000) + '月前'
                  } else if (parseInt((nowTime - date) / 86400000) >= 1) {
                    return parseInt((nowTime - date) / 86400000) + '天前'
                  } else if (parseInt((nowTime - date) / 3600000) >= 1) {
                    return parseInt((nowTime - date) / 3600000) + '小时前'
                  } else if (parseInt((nowTime - date) / 60000) >= 1) {
                    return parseInt((nowTime - date) / 60000) + '分钟前'
                  } else {
                    return '刚刚'
                  }
                }
                for (let i = 0; i < starList.length; i++) {
                  if (starList[i].tip == 'good') {
                    starList[i].type = self.data.columnsGood[starList[i].type];
                  } else {
                    starList[i].type = self.data.columnsBook[starList[i].type];
                  }
                  starList[i].date = handleTime(starList[i].date)
                }
                // res.data 是包含以上定义的两条记录的数组
                self.setData({
                  starList: starList.reverse()
                })
              }
          })
        },
      })
    }
  },
  add_address_fun: function () {
    wx.navigateTo({
      url: 'add_address/add_address',
    })
  },
  deleteCard:function(e){
    console.log(e)
    let self=this;
    wx.showModal({
      title: '提示',
      content: '是否删除这件宝贝',
      success(res) {
        if (res.confirm) {
          substance.doc(e.currentTarget.dataset._id).remove({
            success:res=>{
              wx.showToast({
                title: '成功',
                icon: 'success',
                duration: 2000
              })
              self.onReady();
            },
            fail: console.error
          })
        } else if (res.cancel) {
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let self = this;
    /**
     * 获取用户信息
     */
    wx.cloud.callFunction({
      name: 'login',
      success(res) {
        wx.setStorage({
          key: 'openid',
          data: res.result.openid,
          success: function (res){
            wx.getStorage({
              key: 'openid',
              success: function (res) {
                substance.where({
                  _openid: res.data
                }).get({
                  success: res => {
                    let goodList = res.data
                    let handleTime = (date) => {
                      let nowTime = (new Date()).valueOf()
                      if (parseInt((nowTime - date) / 2592000000) >= 1) {
                        return parseInt((nowTime - date) / 2592000000) + '月前'
                      } else if (parseInt((nowTime - date) / 86400000) >= 1) {
                        return parseInt((nowTime - date) / 86400000) + '天前'
                      } else if (parseInt((nowTime - date) / 3600000) >= 1) {
                        return parseInt((nowTime - date) / 3600000) + '小时前'
                      } else if (parseInt((nowTime - date) / 60000) >= 1) {
                        return parseInt((nowTime - date) / 60000) + '分钟前'
                      } else {
                        return '刚刚'
                      }
                    }
                    for (let i = 0; i < goodList.length; i++) {
                      if (goodList[i].tip == 'good') {
                        goodList[i].type = self.data.columnsGood[goodList[i].type];
                      } else {
                        goodList[i].type = self.data.columnsBook[goodList[i].type];
                      }
                      goodList[i].date = handleTime(goodList[i].date)
                    }
                    // res.data 是包含以上定义的两条记录的数组
                    self.setData({
                      goodList: goodList.reverse()
                    })
                  }
                })
              },
            })
          }
        }) // 3
      }
    })
    wx.getSetting({
      success: res => {
        wx.getUserInfo({
          success: function (res) {
            self.setData({
              userInfo: {
                avatarUrl: res.userInfo.avatarUrl,
                nickName: res.userInfo.nickName
              }
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let self=this
    if (wx.getStorageSync("ifBind")) {
      if (wx.getStorageSync("ifBind") == "isBind") {
        self.setData({
          bind: "已认证"
        })
      }
    }
    wx.getSetting({
      success: res => {
        wx.getUserInfo({
          success: function (res) {
            self.setData({
              userInfo: {
                avatarUrl: res.userInfo.avatarUrl,
                nickName: res.userInfo.nickName
              }
            })
          }
        })
      }
    })
    self.restar();
  },
  restar:function(){
    let self = this
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        star.where({
          _openid: res.data
        }).get({
          success: res => {
            let starList = res.data
            let handleTime = (date) => {
              let nowTime = (new Date()).valueOf()
              if (parseInt((nowTime - date) / 2592000000) >= 1) {
                return parseInt((nowTime - date) / 2592000000) + '月前'
              } else if (parseInt((nowTime - date) / 86400000) >= 1) {
                return parseInt((nowTime - date) / 86400000) + '天前'
              } else if (parseInt((nowTime - date) / 3600000) >= 1) {
                return parseInt((nowTime - date) / 3600000) + '小时前'
              } else if (parseInt((nowTime - date) / 60000) >= 1) {
                return parseInt((nowTime - date) / 60000) + '分钟前'
              } else {
                return '刚刚'
              }
            }
            for (let i = 0; i < starList.length; i++) {
              if (starList[i].tip == 'good') {
                starList[i].type = self.data.columnsGood[starList[i].type];
              } else {
                starList[i].type = self.data.columnsBook[starList[i].type];
              }
              starList[i].date = handleTime(starList[i].date)
            }
            // res.data 是包含以上定义的两条记录的数组
            self.setData({
              starList: starList.reverse()
            })
          }
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
